package com.zr.eim.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.zr.common.constant.SimpleDateFormatConstants;
import com.zr.common.utils.DateUtils;
import com.zr.eim.domain.EimDataPerson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.zr.eim.mapper.EimAnalysisGpsPointMapper;
import com.zr.eim.domain.EimAnalysisGpsPoint;
import com.zr.eim.service.IEimAnalysisGpsPointService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

/**
 * GPS点位Service业务层处理
 * 
 * @author peihang
 * @date 2022-10-21
 */
@Service
public class EimAnalysisGpsPointServiceImpl implements IEimAnalysisGpsPointService 
{


    @Resource
    private MongoTemplate mongoTemplate;

    @Autowired
    private EimAnalysisGpsPointMapper eimAnalysisGpsPointMapper;

    /**
     * 查询GPS点位
     * 
     * @param id GPS点位主键
     * @return GPS点位
     */
    @Override
    public EimAnalysisGpsPoint selectEimAnalysisGpsPointById(Long id)
    {
        return eimAnalysisGpsPointMapper.selectEimAnalysisGpsPointById(id);
    }

    /**
     * 查询GPS点位列表
     * 
     * @param eimAnalysisGpsPoint GPS点位
     * @return GPS点位
     */
    @Override
    public List<EimAnalysisGpsPoint> selectEimAnalysisGpsPointList(EimAnalysisGpsPoint eimAnalysisGpsPoint)
    {
        return eimAnalysisGpsPointMapper.selectEimAnalysisGpsPointList(eimAnalysisGpsPoint);
    }

    /**
     * 新增GPS点位
     * 
     * @param eimAnalysisGpsPoint GPS点位
     * @return 结果
     */
    @Override
    public int insertEimAnalysisGpsPoint(EimAnalysisGpsPoint eimAnalysisGpsPoint)
    {
        eimAnalysisGpsPoint.setCreateTime(DateUtils.getNowDate());
        return eimAnalysisGpsPointMapper.insertEimAnalysisGpsPoint(eimAnalysisGpsPoint);
    }

    /**
     * 修改GPS点位
     * 
     * @param eimAnalysisGpsPoint GPS点位
     * @return 结果
     */
    @Override
    public int updateEimAnalysisGpsPoint(EimAnalysisGpsPoint eimAnalysisGpsPoint)
    {
        return eimAnalysisGpsPointMapper.updateEimAnalysisGpsPoint(eimAnalysisGpsPoint);
    }

    /**
     * 批量删除GPS点位
     * 
     * @param ids 需要删除的GPS点位主键
     * @return 结果
     */
    @Override
    public int deleteEimAnalysisGpsPointByIds(Long[] ids)
    {
        return eimAnalysisGpsPointMapper.deleteEimAnalysisGpsPointByIds(ids);
    }

    /**
     * 删除GPS点位信息
     * 
     * @param id GPS点位主键
     * @return 结果
     */
    @Override
    public int deleteEimAnalysisGpsPointById(Long id)
    {
        return eimAnalysisGpsPointMapper.deleteEimAnalysisGpsPointById(id);
    }




    @Override
    public void addToMongo(EimAnalysisGpsPoint p) {

        mongoTemplate.insert(JSON.toJSONString(p),"GPS"+ SimpleDateFormatConstants.sfShort.format(p.getCreateTime())+"Data");

    }

    @Override
    public EimAnalysisGpsPoint selectLastEimAnalysisGps(EimDataPerson p) {

        Query query = new Query(Criteria.where("personId").is(p.getPersonId()));

        List<String> rl = mongoTemplate.find(query, String.class,"GPS"+SimpleDateFormatConstants.sfShort.format(new Date())+"Data");

        return JSON.parseObject(rl.get(rl.size()-1),EimAnalysisGpsPoint.class);
    }

}
